﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
using System.IO;
using System.Windows.Media;
using ThreeByte.Media;

namespace ThreeByte.ActiveDeck.Config
{

    public class VideoResource
    {
        public Guid ID {
            get;
            private set;
        }

        public string Name {
            get;
            private set;
        }

        public string Path {
            get;
            private set;
        }

        public string MediaType {
            get {
                return "MOV";
            }
        }

        public int Length {
            get {
                return (int)((new FileInfo(System.IO.Path.Combine(PathRoot, Path))).Length);
            }

        }

        private string PathRoot;

        #region Constructors
        private VideoResource() {
            ID = Guid.NewGuid();
        }

        public VideoResource(string name, string path, string pathRoot) : this() {
            Name = name;
            Path = ThreeByte.FileUtilities.PathUtil.RelativePathTo(pathRoot, path);
            PathRoot = pathRoot;
        }

        public VideoResource(Guid id, string name, string path, string pathRoot)
            : this(name, path, pathRoot) {
            ID = id;
        }

        #endregion Constructors

        public XElement ToXml() {
            XNamespace ns = Service.ServiceConfiguration.NAMESPACE;

            XElement node = new XElement(ns + "Media");
            node.Add(new XAttribute("ID", ID));
            node.Add(new XAttribute("Name", Name));
            node.Add(new XAttribute("Path", Path));

            return node;
        }

        public static VideoResource FromXml(XElement node, string pathRoot) {
            Guid id = Guid.Parse(node.Attribute("ID").Value);
            string name = node.Attribute("Name").Value;
            string path = node.Attribute("Path").Value;
            if(System.IO.Path.IsPathRooted(path)) {
                path = ThreeByte.FileUtilities.PathUtil.RelativePathTo(pathRoot, path);
            }
            return new VideoResource(id, name, path, pathRoot);
        }

        public FileStream GetStream() {
            //If the path is relative, make sure to resolve it relative to the presentation directory
            string pathToOpen = System.IO.Path.Combine(PathRoot, Path);
            return new FileStream(pathToOpen, FileMode.Open, FileAccess.Read);
        }

        public XElement GetXmlHeader() {

            XNamespace ns = Service.ServiceConfiguration.NAMESPACE;
            XElement node = new XElement(ns + "Media", new XAttribute("ID", ID.ToString()), new XAttribute("Name", Name ?? string.Empty));
            node.Add(new XAttribute("Type", MediaType));
            //Calculate the size
            node.Add(new XAttribute("Size", Length));

            return node;
        }


        public bool Matches(XElement resourceInfo) {
            try {
                //Verify ID
                if(ID != Guid.Parse(resourceInfo.Attribute("ID").Value)) { return false; }
                //Size
                if(Length != int.Parse(resourceInfo.Attribute("Size").Value)) { return false; }
                //Type
                if(MediaType != resourceInfo.Attribute("Type").Value) { return false; }
                //All tests pass
                return true;
            } catch {
                //Doesn't matter, just false
            }
            return false;
        }
    }
}
